package com.qianji.petmall.order.config;

import com.qianji.petmall.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liang zai
 * @date 2022/5/10 16:47
 * @description
 */
@Configuration
public class MyMQConfig {

    //消费消息

    public void queue(OrderEntity entity, Channel channel, Message message) throws IOException {
        System.out.println("接收到了订单消息:"+entity.getOrderSn());
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        channel.basicAck(deliveryTag,false);
    }

    Map<String, Object> arguments = new HashMap<>();
    @Bean
    public Queue orderDelayQueue(){
        arguments.put("x-message-ttl",60000);
        arguments.put("x-dead-letter-exchange","order-event-exchange");
        arguments.put("x-dead-letter-routing-key","order.release.order");
        Queue queue = new Queue("order.delay.queue",true,false,false,arguments);
        return queue;
    }
    @Bean
    public Queue orderReleaseQueue(){
        Queue queue = new Queue("order.release.queue",true,false,false,null);
        return queue;
    }
    //秒杀服务
    @Bean
    public Queue orderSeckillQueue(){
        Queue queue = new Queue("order.seckill.queue", true, false, false, null);
        return queue;
    }
    //交换机
    @Bean
    public Exchange orderEventExchange(){
        TopicExchange topicExchange = new TopicExchange("order-event-exchange",true,false);
        return topicExchange;
    }
    @Bean
    public Binding orderCreatOrderBinding(){
        Binding binding = new Binding("order.delay.queue", Binding.DestinationType.QUEUE,
                "order-event-exchange", "order.creat.order",null);
        return binding;
    }
    @Bean
    public Binding orderReleaseOrderBinding(){
        Binding binding = new Binding("order.release.queue", Binding.DestinationType.QUEUE,
                "order-event-exchange", "order.release.order",null);
        return binding;
    }
    @Bean
    public Binding orderReleaseOrderQueueBinding(){
        Binding binding = new Binding("stock.release.stock.queue", Binding.DestinationType.QUEUE,
                "order-event-exchange", "order.release.other.#",null);
        return binding;
    }
    @Bean
    //秒杀服务绑定交换机
    public Binding orderSeckillOrderQueueBinding(){
        Binding binding = new Binding("order.seckill.queue", Binding.DestinationType.QUEUE,
                "order-event-exchange", "order.seckill.order", null);
        return binding;
    }

}
